// const charofmark = "scikterp" :
// size(s) click(c) image(i) emote(e) player(p)
// skill(k) item(t) char(r) buff(f) taolu(o)

const IconPath = {
	item: 'guisource/icon/item/100x100/', // t
	skill: 'guisource/icon/skill/80x80/', // k
	buff: 'guisource/icon/skill/80x80/', // f
	char: 'guisource/icon/char/80x80/', // h
	taolu: 'guisource/icon/taolu/80x80/', // o
	char_b: 'guisource/icon/char/150x200/',
};

function getIcon(type: string, id: number | string|number[]): string {
	assert(id != null);
	const icon = IconPath[type];
	if (!icon) {
		return '';
	}
	const name = miscUtils.leftPad(id, 4, '0');
	return `${icon + name}.png`;
}

function getIconFull(type: string, id: number | string | number[]): string {
	return getAssets() + getIcon(type, id);
}

let gImage = null;

class TextBound {
	x = 0;

	y = 0;

	w = 0;

	h = 0;

	size = 0;

	textsize = 0;

	constructor(size: number, x: number, y: number, w: number, h: number, textsize: number) {
		this.size = size;
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		this.textsize = textsize;
	}

	fitImage(d: egret.DisplayObject): void {
		// console.log("fitText",this.y,this.h,d.height)
		// d.y = this.y + this.h - d.height;
		// if (d.width > this.size || d.height > this.h)
		if (!d.width || !d.height) {
			return;
		}
		// console.log("fitImage0",this.x,this.y,this.w,this.h,this.size,this.textsize);
		// console.log("fitImage1",d.x,d.y,d.width,d.height);
		{
			const scalex = this.size / d.width;
			const scaley = this.h / d.height;
			const s = Math.min(scalex, scaley);
			d.scaleX = s;
			d.scaleY = s;
			// console.log("fitImage",this.y,this.h,d.height,d.scaleY)
			d.y = this.y + this.h - d.height * s;
			gImage = d;
		}
		// console.log("fitImage2",d.x,d.y);
	}

	// there are child...
	fitEmote(d: egret.DisplayObject): void {
		// console.log("fitEmote",this.h,d.width,d.height)
		d.x = (this.w - d.width) / 2;
		d.y = this.h - this.textsize;
		// console.log('fitEmotex', this.w, d.width, d.x);
		// console.log('fitEmotey', this.h, d.height, d.y);
	}

	fitAni(d: egret.DisplayObject): void {
		// console.log("fitAni",this.h,d.width,d.height)
		d.x = this.w / 2;
		d.y = this.h;
		if (d.width > this.size || d.height > this.h) {
			const scalex = this.size / d.width;
			const scaley = this.h / d.height;
			const s = Math.min(scalex, scaley);
			d.scaleX = s;
			d.scaleY = s;
		}
	}
}

class AniInText extends ui.Animation {
	textBound: TextBound;

	type: string;

	image: string;

	textsize: number;

	constructor(
		type: string,
		image: string,
		cursize: number,
		x: number,
		y: number,
		w: number,
		h: number,
		textsize: number,
	) {
		super();

		// this.source = image
		this.textsize = textsize;
		this.type = type;
		this.image = image;
		this.textBound = new TextBound(cursize, x, y, w, h, textsize);

		this.x = x;
		this.y = y;

		this.init();
		this.addEventListener('touchTap', this.onTap, this);
	}

	onTap(): void {
		// ui.msgline("you click " + this.type + " " + this.image)
		WestTextEvent.ins().onImageEvent(this.type, this.image);
	}

	async init(): Promise<void> {
		let file = '';
		if (this.type === 'e') {
			const name = miscUtils.leftPad(this.image, 3, '0');
			file = `guisource/emote/${name}`;
		} else if (this.type === 'p') {
			// 'c'用于click了，所以用p!
			// #s(100)#p(110)
			const name = miscUtils.leftPad(this.image, 4, '0');
			file = `shape/char/${name}/stand_0`;
		} else if (this.type === 'a') {
			// #a(scene/ani/dongwu/he)
			file = this.image;
		} else {
			return;
		}

		await this.setAnimation(file);
		if (this.mc) {
			this.updateMC();
		}
	}

	override onLoadedMC(): void {
		this.updateMC();
	}

	updateMC(): void {
		this.mc.play(-1);
		if (this.type === 'e') {
			this.textBound.fitEmote(this.mc);
		} else {
			this.textBound.fitAni(this.mc);
		}
	}
}

class ImageInText extends ui.Image {
	textBound: TextBound;

	type: string;

	imageurl: string;

	constructor(
		type: string,
		imageurl: string,
		cursize: number,
		x: number,
		y: number,
		w: number,
		h: number,
	) {
		super();

		// this.source = image
		this.type = type;
		this.imageurl = imageurl;
		this.textBound = new TextBound(cursize, x, y, w, h, 0);

		assert(isNumber(x) && isNumber(y));

		this.x = x;
		this.y = y;

		// eui.Watcher.watch(this,['x'],this.onChangeX,this);

		// var WatchJS = require("melanke-watchjs")
		// var watch = WatchJS.watch;
		// watch(this, "x", function(){
		// 	console.log("ImageInText.x changed!");
		// });

		this.init();
		this.addEventListener('touchTap', this.onTap, this);
	}

	onChangeX() {
		console.log('onChangeX', this.x);
	}

	onTap(): void {
		// ui.msgline("you click " + this.type + " " + this.imageurl)
		WestTextEvent.ins().onImageEvent(this.type, this.imageurl);
	}

	onComplete(): void {
		this.textBound.fitImage(this);
	}

	async init(): Promise<void> {
		let file: string;
		if (this.type === 'i') {
			file = this.imageurl;
		} else {
			const shortIcon = {
				t: 'item',
				k: 'skill',
				h: 'char',
				f: 'buff',
				o: 'taolu',
			};
			const type = shortIcon[this.type];
			if (!type) {
				return;
			}
			file = getIcon(type, this.imageurl);
		}
		await this.url(file);
		// console.log("fitImageInit",file,this.x,this.y,this.width,this.height,this.source)
		this.onComplete();
	}
}

function CreateImageInText(
	type: string,
	image: string,
	cursize: number,
	x: number,
	y: number,
	w: number,
	h: number,
	textsize: number,
): AniInText | ImageInText {
	assert(isNumber(x) && isNumber(y));
	if (type === 'e' || type === 'p' || type === 'a') {
		return new AniInText(type, image, cursize, x, y, w, h, textsize);
	}
	return new ImageInText(type, image, cursize, x, y, w, h);
}
